package view.Core;

import java.io.BufferedReader;

import org.apache.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;


import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.*;
import android.text.style.UnderlineSpan;
import android.view.*;
import android.widget.*;

public class TabActivityA extends Activity {
	TextView textView1, textView2, textView3, textView4;
	TextView dataTextView;

	ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
	HashMap<String, String> item = new HashMap<String, String>();
	
	String treeID ;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scroll_view2);
		
        Bundle bunde = this.getIntent().getExtras();
        treeID = bunde.getString("treeID");
        
		findViewByID();
		selectAllFromTree();

		SpannableString content = new SpannableString(textView1
				.getText().toString());
		content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
		setAllTexttoDefalut();
		textView1.setText(content);
		
		item = dataList.get(0);
		
		String result;
		result = "Tree ID: " + item.get("treeID")+"\n";
		result = result +  "Registration Date: " +item.get("registrationDate")+"\n";
		result = result + "Lastinspection Date: " +item.get("lastinspectionDate")+"\n";
		//result = result + selectAllByID("district", "distNo" ,"D0001", "distName");
		result = result + "QR code: "+ selectAllByID("qrcode", "treeID" ,item.get("treeID"), "QRcode");
		
		dataTextView.setText(result);
		
		
		
		textView1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SpannableString content = new SpannableString(textView1
						.getText().toString());
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				setAllTexttoDefalut();
				textView1.setText(content);
				
				item = dataList.get(0);
				
				String result;
				result = "Tree ID: " + item.get("treeID")+"\n";
				result = result +  "Registration Date: " +item.get("registrationDate")+"\n";
				result = result + "Lastinspection Date: " +item.get("lastinspectionDate")+"\n";
				//result = result + selectAllByID("district", "distNo" ,"D0001", "distName");
				result = result + "QR code: "+ selectAllByID("qrcode", "treeID" ,item.get("treeID"), "QRcode");
				
				dataTextView.setText(result);
				//toastMsg(item.get("treeID")+"");
			}
		});

		textView2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SpannableString content = new SpannableString(textView2
						.getText().toString());
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				setAllTexttoDefalut();
				textView2.setText(content);
				
				String result;
				result = "DBH: " + item.get("dbh")+" (mm)"+"\n";
				result = result + "Height: "+item.get("height")+ " (m)" +"\n";
				result = result + "Crown Spread: "+item.get("crownSpread")+ " (m)" +"\n";
				result = result + "Estimated Age: "+item.get("estimatedAge")+" Years"+"\n";
				//result = result + item.get("registrationDate")+"\n";
				
				//result = result + selectAllByID("district", "distNo" ,"D0001", "distName");
				//dbh":"50","height":"99","crownSpread":"70","estimatedAge":"100"
				
				dataTextView.setText(result);				
			}
		});
		
		textView3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SpannableString content = new SpannableString(textView3
						.getText().toString());
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				setAllTexttoDefalut();
				textView3.setText(content);
				
				String result="";
				result = result + "Type: "+selectAllByID("type", "typeID" ,item.get("typeID"), "typeName") + '\n';
				result = result + "Characteristic: " + selectAllByID("criteria", "crID" ,item.get("crID"), "characteristic") + '\n';
				result = result + "Status: "+selectAllByID("status", "statusID" ,item.get("statusID"), "status") + '\n';
				
				dataTextView.setText(result);				
			}
		});
		
		
		textView4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				SpannableString content = new SpannableString(textView4
						.getText().toString());
				content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
				setAllTexttoDefalut();
				textView4.setText(content);
				
				String result ;
				result = "Location" +'\n'+ item.get("location");
				dataTextView.setText(result);				
			}
		});
	}

	public String selectAllByID(String inputTable, String inputField, String inputID, String returnField){
		String field = "" ;
		String reply ;
		
		try {
			// String url =
			// "http://192.168.1.101/oldTreeWeb/getJsonField.php?Name=Tree tat";
///getAllByID.php?inputTable=district&inputField=distNo&inputID=D0001
			
			String url = getResources().getString(R.string.serverURL)
					+ "getAllByID.php?inputTable="+ inputTable +"&inputField="+ inputField + "&inputID=" + inputID;
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);

				String getField = json.getString(0);
				
				
				JSONObject jsonField = new JSONObject(getField);
				
	            field = jsonField.getString(returnField); 


			} catch (JSONException e) {
				toastMsg("" + e);
			}
		} catch (Exception e) {
			toastMsg("" + e);
		}
		
		return field;
	}
	
	public void selectAllFromTree() {
		String reply ;
		try {
			// String url =
			// "http://192.168.1.101/oldTreeWeb/getJsonField.php?Name=Tree tat";

			
			String url = getResources().getString(R.string.serverURL)
					+ "getJsonByTreeID.php?inputTreeID=" + treeID;
			// getJsonByTreeID.php
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);

			String line = "";
			reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);

				//toastMsg(""+json.toString());
				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				String getField = jsonArrayB.getString(0);
				
				
				JSONObject jsonField = new JSONObject(getField);
				
	            item.put("treeID", jsonField.getString("treeID")); 
	            item.put("registrationDate", jsonField.getString("registrationDate")); 
	            item.put("location", jsonField.getString("location")); 
	            item.put("dbh", jsonField.getString("dbh")); 
	            
	            item.put("height", jsonField.getString("height")); 
	            item.put("crownSpread", jsonField.getString("crownSpread")); 
	            item.put("estimatedAge", jsonField.getString("estimatedAge")); 
	            item.put("lastinspectionDate", jsonField.getString("lastinspectionDate")); 
	            
	            item.put("typeID", jsonField.getString("typeID")); 
	            item.put("distNo", jsonField.getString("distNo")); 
	            item.put("statusID", jsonField.getString("statusID")); 
	            item.put("crID", jsonField.getString("crID")); 
	            
	            dataList.add(item);
	            
				// textView1.setText(arrayLength);
				// textView1.setText(treeID);
				// textView1.setText(reply);

			} catch (JSONException e) {
				toastMsg("" + e);
			}
		} catch (Exception e) {
			toastMsg("" + e);
		}
	}

	public void toastMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	public void setAllTexttoDefalut() {
		textView1.setText(textView1.getText().toString());
		textView2.setText(textView2.getText().toString());
		textView3.setText(textView3.getText().toString());
		textView4.setText(textView4.getText().toString());
	}

	public void findViewByID() {
		dataTextView = (TextView) findViewById(R.id.dataTextView);
		textView1 = (TextView) findViewById(R.id.textView1);
		textView2 = (TextView) findViewById(R.id.textView2);
		textView3 = (TextView) findViewById(R.id.textView3);
		textView4 = (TextView) findViewById(R.id.textView4);
	}
}